unit uFormMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  uPluginIntf, StdCtrls, Dialogs,
  DrcpSvc;

type
  TForm_Main = class(TForm,IPluginable)
    ListBox_Modules: TListBox;
    Label1: TLabel;
    Label2: TLabel;
    Edit_Id: TEdit;
    Label3: TLabel;
    Label4: TLabel;
    ComboBox_Plugin: TComboBox;
    Label5: TLabel;
    Edit_Name: TEdit;
    Label6: TLabel;
    Memo_Params: TMemo;
    Button_Update: TButton;
    Button_Delete: TButton;
    Button_Refresh: TButton;
    Memo_InitString: TMemo;
    Button_RefreshPluginList: TButton;
    Button_Save: TButton;
    procedure FormDestroy(Sender: TObject);
    procedure Button_RefreshClick(Sender: TObject);
    procedure ListBox_ModulesClick(Sender: TObject);
    procedure Button_DeleteClick(Sender: TObject);
    procedure Button_UpdateClick(Sender: TObject);
    procedure Button_RefreshPluginListClick(Sender: TObject);
    procedure Button_SaveClick(Sender: TObject);
  private
    FSoapAddr : string;
    FSoap : DrcpService;
    { Private declarations }

    function GetCurSelectedModule : Module;
    function GetCurSelectedPlugin : PluginDO;

    procedure RefeshModuleList;
    procedure RefeshPluginList;

    procedure ClearListWithObject(AList : TStrings);
  protected
    FModeCreateNew : Boolean;
    FPlugin : IPlugin;

    procedure SetPlugin(APlugin : IPlugin);
    function GetPlugin:IPlugin;

    procedure Init;
  public


    { Public declarations }
    property Plugin : IPlugin read GetPlugin write SetPlugin;
  end;

var
  Form_Main: TForm_Main;
implementation

{$R *.dfm}

procedure TForm_Main.Init;
var
  lClientConfig : TStrings;
  lPrintPort : String;
begin
  { TODO -oyou -cplugin-template : fill initialization code here }

  //how to get client-config
  lClientConfig := TStringList.Create;
  lClientConfig.Text := Plugin.HostParams;

  lPrintPort := lClientConfig.Values['Port'];

  lClientConfig.Free;


  //how to get soap address by context variable, see more RS_xxx by CTL + LeftClick
  FSoapAddr := Plugin.Variable[RS_DEFAULT_CONTAINER_SOAP_ADDR];
  FSoap := GetDrcpService(false,FSoapAddr);

  //how to get initialzation info
  //Plugin.InitString can be any string, strore in db [module.initstring]

  RefeshPluginList;
  RefeshModuleList;
end;

procedure TForm_Main.FormDestroy(Sender: TObject);
var
  lClientConfig : TStrings;
begin
  lClientConfig := TStringList.Create;
  lClientConfig.Values['Port'] := 'LPT1';
  lClientConfig.Values['X'] := '0';
  lClientConfig.Values['Y'] := '1';

  Plugin.HostParams := lClientConfig.Text;  //save to db

  lClientConfig.Free;

  { TODO -oyou -cplugin-template : fill finalization code here }
  
end;

procedure TForm_Main.Button_RefreshClick(Sender: TObject);
begin
  RefeshModuleList;
  
end;

procedure TForm_Main.ListBox_ModulesClick(Sender: TObject);
var
  I : integer;
  lModule : Module;
  lRole : Role;
  lJoinedRoleList,
  lAllRoleList : ArrayOf_tns2_Role;
  lPluginDo : PluginDO;
begin

  lModule := Module(ListBox_Modules.Items.Objects[ListBox_Modules.ItemIndex]);


  lPluginDo := FSoap.Plugin_Get(lModule.pluginId);
  if lPluginDo <> nil then
    ComboBox_Plugin.ItemIndex := ComboBox_Plugin.Items.IndexOf(lPluginDo.name)
  else
    ComboBox_Plugin.ItemIndex := -1;


  Edit_Id.Text := lModule.id;
  Edit_Name.Text := lModule.name;
  Memo_Params.Lines.Text := lModule.param;
  Memo_InitString.Lines.Text := lModule.initString;
end;

procedure TForm_Main.Button_DeleteClick(Sender: TObject);
var
  lModule : Module;
begin
  lModule := Module(ListBox_Modules.Items.Objects[ListBox_Modules.ItemIndex]);
  FSoap.Module_Delete(lModule.id);

  RefeshModuleList;
end;

procedure TForm_Main.RefeshModuleList;
var
  I : integer;
  lModule : Module;
  lAllModules : ArrayOf_tns2_Module;
begin

  ClearListWithObject(ListBox_Modules.Items);

  lAllModules := FSoap.Module_GetAll;
  for I := 0 to Length(lAllModules) -1 do
  begin
    lModule := lAllModules[I];
    ListBox_Modules.Items.AddObject(lModule.name,lModule);
  end;
end;

procedure TForm_Main.Button_UpdateClick(Sender: TObject);
var
  lModule : Module;
  lPlugin : PluginDO;
begin

  lModule := Module.Create;

  lModule.id := Edit_Id.Text;
  lModule.name := Edit_Name.Text;
  lModule.pluginId := GetCurSelectedPlugin.id;
  lModule.param := Memo_Params.Lines.Text;
  lModule.initString := Memo_InitString.Lines.Text;

  FSoap.Module_Update(lModule);


  RefeshModuleList;
end;

function TForm_Main.GetCurSelectedModule: Module;
begin
  Result := Module(ListBox_Modules.Items.Objects[ListBox_Modules.ItemIndex]);
end;

procedure TForm_Main.ClearListWithObject(AList: TStrings);
var
  I : integer;
begin
  for I := 0 to AList.Count -1 do
    (AList.Objects[I]).Free;

  AList.Clear;
end;


procedure TForm_Main.RefeshPluginList;
var
  I : integer;
  lPlugin : PluginDO;
  lAllPlugins : ArrayOf_tns2_PluginDO;
begin

  ClearListWithObject(ComboBox_Plugin.Items);

  lAllPlugins := FSoap.Plugin_GetAll(false);
  for I := 0 to Length(lAllPlugins) -1 do
  begin
    lPlugin := lAllPlugins[I];
    ComboBox_Plugin.Items.AddObject(lPlugin.name,lPlugin);
  end;
end;

function TForm_Main.GetCurSelectedPlugin: PluginDO;
begin
  Result := PluginDO(ComboBox_Plugin.Items.Objects[ComboBox_Plugin.ItemIndex]);
end;

procedure TForm_Main.Button_RefreshPluginListClick(Sender: TObject);
begin
  RefeshPluginList;
end;

procedure TForm_Main.Button_SaveClick(Sender: TObject);
var
  lModule : Module;
  lPlugin : PluginDO;
begin
  lModule := Module.Create;

  lModule.name := Edit_Name.Text;
  lModule.pluginId := GetCurSelectedPlugin.id;
  lModule.param := Memo_Params.Lines.Text;
  lModule.initString := Memo_InitString.Lines.Text;

  FSoap.Module_Save(lModule);


  RefeshModuleList;

end;

function TForm_Main.GetPlugin: IPlugin;
begin
  Result := FPlugin;
end;

procedure TForm_Main.SetPlugin(APlugin: IPlugin);
begin
  FPlugin := APlugin;

  Init;
end;

end.

